import axios from  'axios'

//新建个axios对象
const httpClient = axios.create({
  validateStatus(status) {
    return status >= 200 && status <= 504 //设置默认的合法状态码，不合法的话不接收response
  },
  timeout: 10000 //超时时间10s
})


httpClient.defaults.retry = 3 //请求重试次数
httpClient.defaults.retryDelay = 1000 //请求重试时间间隔
httpClient.defaults.shouldRetry = true //是否重试

// 添加请求拦截器
httpClient.interceptors.request.use(
  config => {
    //添加header
    config.headers['Content-Type'] = 'application/json'
    config.headers['Accept-Language'] = 'zh-CN'
    config.headers['Authorization'] = localStorage.getItem('token')
    if (config.method === 'post'){
      if(!config.data) {
        config.data = {}
      }
    }
    return config
  },
  err => {
    //Promise.reject()方法返回一个带有拒绝原因的Promise对象，在F12的console中显示报错
    Promise.reject(err)
  }
);

// 添加响应拦截器
httpClient.interceptors.response.use(
  response => {
    if(response.status !== 200){
      return Promise.reject(response.data)
    } else {
      return response.data
    }
  },
  err => {
    return Promise.reject(err)
  }
);

export default httpClient;